package com.zuipin.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

public class ObEntity extends TableEntity {
	private String					obName;
	private List<PropertyEntity>	propertyEntityList;
	
	public String getObName() {
		return obName;
	}
	
	public void setObName(String obName) {
		this.obName = obName;
	}
	
	public List<PropertyEntity> getPropertyEntityList() {
		return propertyEntityList;
	}
	
	public void setPropertyEntityList(List<PropertyEntity> propertyEntityList) {
		this.propertyEntityList = propertyEntityList;
	}
	
	public String getRemark() {
		if (getRemarks() != null && getRemarks().contains("表")) {
			return getRemarks().replaceAll("表", "");
		} else {
			return getRemarks();
		}
	}
	
	public String getClassName() {
		return CommonUtils.getUpFirstName(obName);
	}
	
	public static ObEntity convert2ObEntity(TableEntity te) {
		ObEntity ob = new ObEntity();
		if (te != null) {
			BeanUtils.copyProperties(te, ob);
			ob.setObName(CommonUtils.toCamelCase(te.getTalbleName()));
			ob.setRemarks(te.getRemarks());
			List<PropertyEntity> peList = new ArrayList<PropertyEntity>();
			for (ColumnEntity ce : te.getColumnlist()) {
				PropertyEntity pe = new PropertyEntity();
				BeanUtils.copyProperties(ce, pe);
				pe.setPropName(ce.getColumnName());
				pe.setPropType(CommonUtils.getCodeType(ce.getColumnType()));
				peList.add(pe);
			}
			ob.setPropertyEntityList(peList);
		}
		return ob;
	}
	
	public static List<ObEntity> convert2ObEntityList(List<TableEntity> teList) {
		List<ObEntity> oeList = null;
		if (teList != null) {
			oeList = new ArrayList<ObEntity>();
			for (TableEntity te : teList) {
				oeList.add(convert2ObEntity(te));
			}
		}
		return oeList;
	}
	
	public String getPKName() {
		StringBuffer sbuffer = new StringBuffer();
		if (propertyEntityList != null && propertyEntityList.size() > 0) {
			for (PropertyEntity pe : propertyEntityList) {
				if (pe.getPkType() == 1) {
					sbuffer.append(pe.getMethodName());
				}
			}
			
		}
		return sbuffer.toString();
	}
}
